document.addEventListener('DOMContentLoaded', function () {


    //模拟封装jQuery $
    function $key(getkeys) {
        return document.querySelector(getkeys);
    }


    // 提取日期相关函数
    date();
    function date() {
        var spa = document.querySelector(".dater");
        var timer = null;
        // 封装时间日期格式化
        function mydate() {
            var date = new Date();
            // 时间日期格式化 yyyy-mm-dd hh:mm:ss
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var data = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            // 补 0
            month = (month < 10) ? "0" + month : month;
            data = (data < 10) ? "0" + data : data;
            hours = (hours < 10) ? "0" + hours : hours;
            minutes = (minutes < 10) ? "0" + minutes : minutes;
            seconds = (seconds < 10) ? "0" + seconds : seconds;
            var res = `${year}年${month}月${data}日 ${hours}:${minutes}:${seconds}`;
            return res;
        }

        function automove() {
            spa.innerHTML = mydate();
        }
        // 自动走
        setInterval(automove, 1000);
    }


    //添加内容
    var audittop = document.getElementsByClassName("audit-top")[0];
    audittop.innerHTML = `
            <div class="Lines"></div>
            <p class="penc">审核系统</p>
    `
    var auditottom = document.getElementsByClassName("audit-bottom")[0];
    auditottom.innerHTML = `
        <table>
              <thead>
                <tr>
                  <th>姓名</th>
                  <th>请假事由</th>
                  <th>职能</th>
                  <th>请假时间</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody class="keria"></tbody>
              <tfoot>
                <tr>
                  <th  colspan="5">
                    <button id="uper6">&lt;</button>
                    <div class="age6"></div>
                    <button id="nexter6">&gt;</button>
                  </th>
                </tr>
              </tfoot>
            </table>
    `


    //AJAX调接口
    var pageIndex = 1;
    var pageSize = 10;
    function auditList() {
        $key(".keria").innerHTML = "";
        $key(".age6").innerHTML = "";
        var lh = {
            pageIndex: pageIndex,
            pageSize: pageSize
        }
        ajax.post("http://192.168.199.101:8080/BaorSystem/audit/auditList", lh, function (res) {
            if (res.code == 200) {
                console.log(res);
                pageCount = res.dataset.pageCount;
                for (var i = 0; i < pageCount; i++) {
                    //创建元素
                    btn = document.createElement("button");
                    btn.innerHTML = i + 1;
                    var age6 = document.getElementsByClassName("age6")[0];
                    age6.appendChild(btn);
                }
                for (var item of res.dataset) {
                    var newTr6 = document.createElement("tr");
                    newTr6.innerHTML = `
                        <td>${item.userId}</td>
                        <td>${item.vacationCount}</td>
                        <td>${item.identify}</td>
                        <td>${item.vacationDate}</td>
                        <td>
                            <button class="btno1">通过</button>
                            <button class="btno2">不通过</button>
                        </td>
                    `
                    $key(".keria").appendChild(newTr6);
                    var blo1 = document.getElementsByClassName("btno1")[0];
                    var blo2 = document.getElementsByClassName("btno2")[0];
                    blo1.onclick = function () {
                        var data = {
                            vacationId: 1,
                            status: 1,
                            examine: null,
                        }
                        ajax.post("http://192.168.199.101:8080/BaorSystem/audit/auditActions", data, function (res) {
                            console.log(res);
                        })
                    }
                    blo2.onclick = function () {
                        var data = {
                            vacationId: 1,
                            status: 0,
                            examine: null,
                        }
                        ajax.post("http://192.168.199.101:8080/BaorSystem/audit/auditActions", data, function (res) {
                            console.log(res);
                        })
                    }
                }
            }
        })
    }
    auditList();
    pagination()




    //分页
    function pagination() {
        $key("#nexter6").onclick = function () {//下一页
            pageIndex++;
            if (pageIndex >= pageCount) {
                pageIndex = pageCount;
                this.disabled = true;
            }
            $key("#uper6").disabled = false;
            // btn.style.backgroundColor="red";
            auditList();
        }
        $key("#uper6").onclick = function () {//上一页
            pageIndex--;
            if (pageIndex <= 1) {
                pageIndex = 1;
                this.disabled = true;
            }
            $key("#nexter6").disabled = false;
            auditList();
        }
    }


    //页面跳转
    //日志系统
    var journal = document.getElementsByClassName("journal")[0];
    journal.onclick = function () {
        location.href = "./diary.html";
    }
    //审核系统
    var vet = document.getElementsByClassName("vet")[0];
    vet.onclick = function () {
        location.href = "./audit.html";
    }
    //用户管理
    var user = document.getElementsByClassName("user")[0];
    user.addEventListener('click', function () {
        location.href = "./UserMage.html";
    });
    //班级管理
    var classs = document.getElementsByClassName("classs");
    classs[0].onclick = function () {
        location.href = "./ClassManagement.html";
    }
    //日常
    var routine = document.getElementsByClassName("routine")[0];
    routine.onclick = function () {
        location.href = "./daily.html";
    }
    //学生档案
    var archives = document.getElementsByClassName("archives")[0];
    archives.onclick = function () {
        location.href = "./student file.html";
    }
    //项目管理
    var project = document.getElementsByClassName("project")[0];
    project.onclick = function () {
        location.href = "./projectmanagement.html";
    }
    //出差记录
    var travel = document.querySelector(".travel");
    travel.addEventListener('click', function () {
        location.href = "./business.html";
    });
    //请假记录
    var excused = document.querySelector(".excused");
    excused.addEventListener('click', function () {
        location.href = "./vacate.html";
    });
    //考勤记录
    var presence = document.querySelector(".presence");
    presence.addEventListener('click', function () {
        location.href = "./clock.html";
    });
});